<!--
 * @Author:
 * @Date: 2023-02-28 16:12:52
 * @LastEditors:
 * @LastEditTime: 2023-08-16 18:15:05
 * @FilePath: \immoka-admin\src\views\roomGift\fullBroadcastManagement.vue
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name} , All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;新增&emsp;</el-button>
          <el-button type="danger" @click="handleDeleteAll">批量删除</el-button>
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table ref="dataList" :data="tableList" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  border row-key="id"
                  style="width:100%;margin-bottom: 20px;"
                  @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="序号" prop="id"></el-table-column>
          <el-table-column label="广播名称" prop="name"></el-table-column>
          <el-table-column label="分类状态" prop="status">
            <template slot-scope="scope">
              <span>{{ scope.row.status === 0 ? '禁用': '启用' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="广播展示图" prop="picture">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.picture" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.picture" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="添加时间" prop="created_at"></el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini" type="primary" @click="handleUpdate(scope.row)">编辑
              </el-button>
              <el-button icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <cus-pagination
      :limit.sync="listQuery.page_size"
      :page.sync="listQuery.page"
      :total="total"
      @pagination="getList"
    />


    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="40%"
    >
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
            <el-button @click="deleteDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmDelete">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose('editRulesForm')"
      width="30%"
    >
      <el-form ref="editRulesForm" :model="editRulesForm" :rules="editRulesFormRules" class="demo-ruleForm"
               label-width="150px"
      >
        <el-form-item label="广播名称" prop="name">
          <el-input v-model="editRulesForm.name" placeholder="请输入广播名称"></el-input>
        </el-form-item>

        <el-form-item label="广播展示图" prop="picture">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :before-upload="beforeAvatarUpload"
            :file-list="specialFlagBigList"
            :http-request="customizeUpload"
            :on-change="handleChangePic"
            :on-remove="handleSpecialFlagBigRemove"
            :on-success="handleSpecialFlagBigSuccess"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传广播展示图</el-button>
          </el-upload>
        </el-form-item>

        <el-form-item label="送礼用户文本颜色" prop="send_user_text">
          <el-input v-model="editRulesForm.send_user_text" placeholder="请输入送礼用户文本颜色"></el-input>
        </el-form-item>
        <el-form-item label="收礼用户文本颜色" prop="user_text">
          <el-input v-model="editRulesForm.user_text" placeholder="请输入收礼用户文本颜色"></el-input>
        </el-form-item>
        <el-form-item label="房间名称文本颜色" prop="room_text">
          <el-input v-model="editRulesForm.room_text" placeholder="请输入房间名称文本颜色"></el-input>
        </el-form-item>
        <el-form-item label="礼物名称文本颜色" prop="gift_text">
          <el-input v-model="editRulesForm.gift_text" placeholder="请输入礼物名称文本颜色"></el-input>
        </el-form-item>
        <el-form-item label="礼物数量文本颜色" prop="gift_num_text">
          <el-input v-model="editRulesForm.gift_num_text" placeholder="请输入礼物数量文本颜色"></el-input>
        </el-form-item>
        <el-form-item label="默认文本颜色" prop="default_text">
          <el-input v-model="editRulesForm.default_text" placeholder="请输入默认文本颜色"></el-input>
        </el-form-item>
        <el-form-item label="去围观文本颜色" prop="onlookers_text">
          <el-input v-model="editRulesForm.onlookers_text" placeholder="请输入去围观文本颜色"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="editRulesForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listData, addBroadcasts, editBroadcasts, delBroadcasts } from '@/api/roomGift/fullBroadcast'
import { getCateData } from '@/api/personalizedMall/commodity'

export default {
  name: 'wealthDress',
  data() {
    return {
      confirmBtn: false,
      specialFlagBigList: [],
      specialFlagBigList2: [],
      specialFlagBigList3: [],
      specialFlagBigList4: [],
      categoryOption: [],
      // 要修改的当前行的id
      row_id: 0,
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
        name: [{ required: true, message: '请输入广播名称', trigger: 'blur' }],
        picture: [{ required: true, message: '请上传广播展示图', trigger: 'blur' }]
      },

      editRulesForm: {
        id: undefined,
        picture: undefined,
        name: undefined,
        status: undefined,
        send_user_text: undefined,
        user_text: undefined,
        room_text: undefined,
        gift_text: undefined,
        gift_num_text: undefined,
        default_text: undefined,
        onlookers_text: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      tableList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ''
    }
  },
  created() {
    this.getList()
    this.handleGetCateData()
  },
  methods: {
    handleChangePic(fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
    },
    handleGetCateData() {
      getCateData().then(res => {
        this.categoryOption = [...this.categoryOption, ...res.data]
        this.addCategoryOption = res.data
      })
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/webp' || file.type === 'image/gif'
      // const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //   this.$message.error('请上传JPG/PNG格式的图片!')
      // }
      // if (!isLt2M) {
      //   this.$message.error('上传图片大小不能超过 2MB!')
      // }
      // return isJPG && isLt2M
    },
    handleSpecialFlagBigSuccess() {
      this.editRulesForm.picture = this.ossLastImgUrl
      this.specialFlagBigList = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove() {
      this.specialFlagBigList = []
    },
    getList() {
      this.listLoading = true
      listData(this.listQuery).then((response) => {
        this.tableList = response.data.data
        this.listLoading = false
        this.confirmBtn = false
        this.total = response.data.total
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      // row)
      this.row_id = row.id
      this.editRulesForm = {
        id: row.id,
        picture: row.picture,
        name: row.name,
        status: row.status,
        send_user_text: row.send_user_text,
        user_text: row.user_text,
        room_text: row.room_text,
        gift_text: row.gift_text,
        gift_num_text: row.gift_num_text,
        default_text: row.default_text,
        onlookers_text: row.onlookers_text
      }
      this.specialFlagBigList = [{ url: row.picture }]
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.editRulesForm = {
          id: undefined,
          picture: undefined,
          title: undefined,
          status: undefined,
          send_user_text: undefined,
          user_text: undefined,
          room_text: undefined,
          gift_text: undefined,
          gift_num_text: undefined,
          default_text: undefined,
          onlookers_text: undefined
        }
      })

      this.specialFlagBigList = []
      this.specialFlagBigList2 = []
      this.specialFlagBigList3 = []
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    submitForm(formName) {
      if (this.confirmBtn) return

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          if (this.dialogStatus === 'create') {
            addBroadcasts(this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false
              }
            })
          } else {
            editBroadcasts(this.editRulesForm,this.row_id).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false
              }
            })
          }
        } else {
          // 'error submit!!');
          return false
        }
      })
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { id: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { id: ids }
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      // this.dataIds)
      delBroadcasts(this.dataIds).then((response) => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-upload-list__item {
    transition: none !important;
  }
  .el-upload-list--picture {
    max-height: 103px !important;
    overflow: hidden;
  }
}
</style>
